package com.wisdytech.linkdcs.pda.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.linkdcs.common.BaseUtils;
import com.wisdytech.linkdcs.common.PojoConvertUtil;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.pda.dto.CheckUserDto;
import com.wisdytech.linkdcs.pda.service.ISystemService;
import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * 用户登录相关
 * @author dlb
 *
 */
@RestController()
@RequestMapping("/system")
public class SystemAction {
	
	@Resource
	ISystemService systeService;
	
	private Logger logger =LoggerFactory.getLogger(getClass());
	
	
	@RequestMapping("/userLogin")
	public StandardResult userLogin(String username ,String password) {
		
		boolean loginResult =systeService.login(username, password);
		if(!loginResult) {
			logger.error("登陆账号密码错误！ 账号为：{}，密码为：{}",username,password);
			return StandardResult.getFailResult("登陆失败");
		}
		
		List<String> resources= systeService.getUserResource(username);
		Map<String,Object> result =BaseUtils.getRetResult("resouceList", resources);
		
		return StandardResult.getSuccessResult(result);
	}
	
	@RequestMapping("/getCheckUserList")
	public StandardResult getCheckUserList() {
		List<SysUser> users =systeService.getUsers();
		List<CheckUserDto> checkUsers =PojoConvertUtil.convertPojos(users, CheckUserDto.class);
		Map<String,Object> result =BaseUtils.getRetResult("listData",checkUsers);
		return StandardResult.getSuccessResult(result);
	}
	
	@RequestMapping("/getEquipDetail")
	public StandardResult getEquipDetail(String equipCode) {
		if(StringUtils.isBlank(equipCode)) {
			logger.error("设备编码为空");
			return StandardResult.getFailResult("设备编码为空");
		}
		
		Equipment equipment =systeService.getEquipDetail(equipCode);
		if(null == equipment) {
			return StandardResult.getFailResult("设备不存在");
		}
		
		Map<String,Object> result =BaseUtils.getRetResult("equipName",equipment.getEquipName());
		return StandardResult.getSuccessResult(result);
	}
	
	
}
